package com.websuites.web.filter;

import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("unchecked")
public class ResponseHeaderFilter implements Filter
{
	private FilterConfig filterConfig = null;

	public void init(FilterConfig config) throws ServletException
	{
		filterConfig = config;
	}

	public void doFilter(ServletRequest request, ServletResponse response,
	  FilterChain filterChain) throws IOException, ServletException
	{
		HttpServletResponse res = (HttpServletResponse) response;

		for (Enumeration e = filterConfig.getInitParameterNames();e.hasMoreElements();)
		{
			String headerName = (String) e.nextElement();

			res.addHeader(headerName, filterConfig.getInitParameter(headerName));
		}

		filterChain.doFilter(request, response);
	}

	public void destroy()
	{
		filterConfig = null;
	}

	public FilterConfig getFilterConfig()
	{
		return filterConfig;
	}
}